/*
 *  Copyright (C) 2010 Martin Haulrich <mwh.isv@cbs.dk>
 *
 *  This file is part of the MatrixParser package.
 *
 *  The MatrixParser program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.osdtsystem.matrixparser.parsers.loss;

import org.osdtsystem.matrixparser.data.CONLLToken;
import org.osdtsystem.matrixparser.parsers.output.DependencyTree;
import org.osdtsystem.matrixparser.logging.Log;

/**
 *
 * @author Martin Haulrich
 */
public class LabelledLoss implements Loss {
    double wrongHeadLoss;
    double wrongLabelLoss;

    public LabelledLoss() {
        this(1, 1);
    }
    
    public LabelledLoss(double wrongHeadLoss, double wrongLabelLoss) {
        this.wrongHeadLoss = wrongHeadLoss;
        this.wrongLabelLoss = wrongLabelLoss;
    }

    public double loss(DependencyTree gold, DependencyTree system) {
        // Check arguments
        if (gold.sentence() != system.sentence())
            throw new IllegalArgumentException("Trying to compare parses for two different sentences!");
        
        // Compute loss
        double loss = 0;
        for (int node = 1; node < system.nodes(); node++) {
            if (gold.head(node) != system.head(node)) {
                loss += wrongHeadLoss;
            } else if (gold.label(node) != system.label(node)) {
                loss += wrongLabelLoss;
            }
        }
        return loss;
    }

    public void setLossForIncorrectLabel(double labelLoss) {
        if (labelLoss < 0 || labelLoss > 1)
            throw new IllegalArgumentException("Label loss must be within the interval from 0 to 1");
        wrongLabelLoss = labelLoss;
        Log.config("LabelledLoss: setting loss for incorrect labels to " + wrongLabelLoss);
    }
}
